package com.llkj.qingnianguoxue.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.base.BaseActivity;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.StringUtils;
import com.llkj.qingnianguoxue.util.ToastUtil;

/**
 * 忘记密码
 */
public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener {
    private EditText etPhone, etCode, etPwd, etSurePwd;//手机，验证码，密码，邀请码
    private TextView tvGetCode, btnSubmit;//获取验证码,提交
    private TimeCount time;//倒计时
    private int durationTime = 60 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        setTitle("找回密码", true, TITLE_TYPE_IMG, R.drawable.icon_back, false, -1, -1);

        initView();
        initListener();

        time = new TimeCount(durationTime, 1000);
    }

    private void initView() {
        etPhone = (EditText) findViewById(R.id.et_phone);
        etCode = (EditText) findViewById(R.id.et_code);
        etPwd = (EditText) findViewById(R.id.et_pwd);
        etSurePwd = (EditText) findViewById(R.id.et_ensure_pwd);
        tvGetCode = (TextView) findViewById(R.id.tv_get_code);
        btnSubmit = (TextView) findViewById(R.id.tv_submit);
    }

    private void initListener() {
        tvGetCode.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String phone = null;
        switch (v.getId()) {
            case R.id.tv_get_code:
                phone = etPhone.getText().toString().trim();
                if (!StringUtils.isPhoneNumber(phone)) {
                    ToastUtil.makeLongText(this, "请输入正确的手机号格式");
                    return;
                }
                time.start();

                loadWaitDialog();
                HttpMethod.loginFormcode(this, this, phone,"2");
                break;
            case R.id.tv_submit:
                phone = etPhone.getText().toString().trim();
                if (!StringUtils.isPhoneNumber(phone)) {
                    ToastUtil.makeLongText(this, "请输入正确的手机号格式");
                    return;
                }
                String code = etCode.getText().toString();
                if (StringUtils.isEmpty(code)) {
                    ToastUtil.makeLongText(this, "请输入验证码");
                    return;
                }
                String pwd = etPwd.getText().toString();
                if (pwd.length() < 6) {
                    ToastUtil.makeLongText(this, "请输入至少6位密码");
                    return;
                }
                String sure = etSurePwd.getText().toString();
                if(!pwd.equals(sure)){
                    ToastUtil.makeLongText(this, getString(R.string.check_ensure_pwd));
                    return;
                }

                loadWaitDialog();
                HttpMethod.findPassword(this,this,phone,code,pwd);
                break;
        }
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_LOGIN_FIND_PASSWORD) {//找回密码
            ToastUtil.makeLongText(this,getString(R.string.find_pwd_success));
            finish();
        }else if(method == HttpMethod.HTTP_LOGIN_FORMCODE){//获取验证码
            ToastUtil.makeLongText(this,getString(R.string.send_code_success));
        }
    }

    @Override
    protected void onhttpFailed(String message, int method) {
        super.onhttpFailed(message, method);
        if(!StringUtils.isEmpty(message)){
            ToastUtil.makeLongText(this,message);
        }
    }

    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            if (tvGetCode != null) {
                tvGetCode.setText("重新发送");
                tvGetCode.setClickable(true);
            }
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            if (tvGetCode != null) {
                tvGetCode.setClickable(false);//防止重复点击
                tvGetCode.setText(millisUntilFinished / 1000 + "s");
            }
        }
    }
}
